package com.zb.jsp.action;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.zb.jsp.service.AdvertAddressService;
import com.zb.jsp.utils.CopyFile;
import com.zb.jsp.utils.GetExtension;
import com.zb.jsp.utils.IdsLink;
import com.zb.jsp.vo.AdvertAddressVo;

public class AdvertAddressAction extends ActionSupport {

	// 用于注册广告位的vo
	private AdvertAddressVo advertAddressVo;
	// 用于查询广告位的vo
	private AdvertAddressVo advertAddressVoQry;
	// 广告位信息的查询结果列表
	private List<AdvertAddressVo> advertAddressVoList;
	// 用于保存页面id序列的数组
	private String[] selectId;
	// 保存/更新按钮控制FLG
	private String butFlgVal;

	private AdvertAddressService advertAddressService = new AdvertAddressService();

	File uploadFile;
	String uploadFileFileName;
	String uploadFileContentType;

	String uploadFoldPath = ServletActionContext.getServletContext()
			.getRealPath("/advertAddressUploadFile");
	File uploadFold = new File(uploadFoldPath);

	// 默认执行方法
	public String execute() {
		if (!uploadFold.isDirectory()) {
			uploadFold.mkdirs();
		}
		uploadFileFileName = new Date().getTime()
				+ GetExtension.getExtension(uploadFileFileName);

		// 目标文件的文件路径
		String destPath = uploadFoldPath + "/" + uploadFileFileName;
		// 根据目标文件路径创建目标文件
		File dest = new File(destPath);

		CopyFile.copy(uploadFile, dest);

		advertAddressVo.setDefPath(uploadFileFileName);
		// advertAddressVo.setDefPath();
		advertAddressService.addAdvertAddress(advertAddressVo);
		// 保存数据到数据库中
		return "addAdvertAddressSuccess";
	}

	/*
	 * public void validate(){ if(!uploadFileContentType.equals("image/jpeg")){
	 * addFieldError("fielderrormassege", "图片格式不对 请输入jpg格式图片 "); } }
	 */

	/**
	 * 广告位查询
	 * 
	 * @return 查询结果跳转
	 */
	public String searchAdvertAddress() {
		// 根据页面输入的查询条件查询广告位信息列表
		advertAddressVoList = advertAddressService
				.findAdvertAddressVoListByAdvertAddressVoQry(advertAddressVoQry);
		return "searchAdvertAddressSuccess";
	}

	/**
	 * 根据id删除广告位的信息
	 * 
	 * @return
	 */
	public String delAdvertAddress() {

		// 根据页面获取到的id值删除数据库中相应的信息
		String ids = IdsLink.idsLink(selectId);
		advertAddressService.delAdvertAddress(ids);
		System.out.println("删除成功");
		return searchAdvertAddress();
	}

	/**	
	 * 用于修改前将信息显示在修改页面的查询		
	 * @return 返回更新成功标志
	 */
	public String initUptAdvertAddress() {
		// 根据ID查询广告位信息
		advertAddressVo = advertAddressService
				.findTblAdvertAddressById(advertAddressVo.getId());
		butFlgVal = "upt";
		return "initUptAdvertAddress";
	}

	/**
	 * 更新广告位信息
	 * 
	 * @return
	 */
	public String uptAdvertAddress() {
		
		if (uploadFile != null) {
			if (!uploadFold.isDirectory()) {
				uploadFold.mkdirs();
			}
			uploadFileFileName = new Date().getTime()
					+ GetExtension.getExtension(uploadFileFileName);

			// 目标文件的文件路径
			String destPath = uploadFoldPath + "/" + uploadFileFileName;
			// 根据目标文件路径创建目标文件
			File dest = new File(destPath);

			CopyFile.copy(uploadFile, dest);

			advertAddressVo.setDefPath(uploadFileFileName);
		}

		// 更新广告位信息到数据库中
		advertAddressService.uptAdvertAddress(advertAddressVo);
		return "uptAdvertAddressSuccess";
	}

	public AdvertAddressVo getAdvertAddressVo() {
		return advertAddressVo;
	}

	public void setAdvertAddressVo(AdvertAddressVo advertAddressVo) {
		this.advertAddressVo = advertAddressVo;
	}

	public AdvertAddressVo getAdvertAddressVoQry() {
		return advertAddressVoQry;
	}

	public void setAdvertAddressVoQry(AdvertAddressVo advertAddressVoQry) {
		this.advertAddressVoQry = advertAddressVoQry;
	}

	public List<AdvertAddressVo> getAdvertAddressVoList() {
		return advertAddressVoList;
	}

	public void setAdvertAddressVoList(List<AdvertAddressVo> advertAddressVoList) {
		this.advertAddressVoList = advertAddressVoList;
	}

	public String[] getSelectId() {
		return selectId;
	}

	public void setSelectId(String[] selectId) {
		this.selectId = selectId;
	}

	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getUploadFileFileName() {
		return uploadFileFileName;
	}

	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}

	public String getUploadFileContentType() {
		return uploadFileContentType;
	}

	public void setUploadFileContentType(String uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}

	public String getButFlgVal() {
		return butFlgVal;
	}

	public void setButFlgVal(String butFlgVal) {
		this.butFlgVal = butFlgVal;
	}
}
